package com.huage.courseplatform.application.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huage.courseplatform.application.model.dto.PeriodStudentCommentDTO;
import com.huage.courseplatform.application.model.entity.PeriodStudentComment;
import com.huage.courseplatform.application.model.param.PeriodStudentCommentPARAM;
import com.huage.courseplatform.application.model.vo.PeriodStudentCommentAndTeacherVO;
import com.huage.courseplatform.application.service.PeriodStudentCommentService;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.api.AbstractController;
import com.huage.courseplatform.framework.api.BaseController;
import com.huage.courseplatform.framework.model.param.PagePARAM;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.model.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/periodStudentComment")
@Api(tags = "学生评价模板接口（被评价对象为学生）")
@Slf4j
public class PeriodStudentCommentController extends AbstractController<PeriodStudentComment, PeriodStudentCommentDTO, PeriodStudentCommentPARAM> {

    @Resource
    PeriodStudentCommentService  periodStudentCommentService;

    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
    @ApiOperation(value = "添加")
    @PostMapping(value = "/add")
    @RepeatSubmit
    public ResultUtils<PeriodStudentCommentDTO> add(@RequestBody @Validated({Insert.class}) PeriodStudentCommentPARAM param) {
        PeriodStudentComment entity = this.paramToEntity(param);
        periodStudentCommentService.save(entity);
        return ResultUtils.success("添加成功",entityToDto(entity));
    }

//    @ApiOperation(value = "更新")
//    @PutMapping(value = "/update")
//    @RepeatSubmit
//    public ResultUtils<PeriodStudentCommentDTO> update(@RequestBody @Validated({Update.class}) PeriodStudentCommentPARAM param) {
//        PeriodStudentComment entity = this.paramToEntity(param);
//        periodStudentCommentService.saveOrUpdate(entity);
//        return ResultUtils.success("更新成功",entityToDto(entity));
//    }

    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
    @ApiOperation(value = "根据id删除")
    @DeleteMapping(value = "/delete/{id}")
    public ResultUtils<String> deleteById(@PathVariable String id) {
        periodStudentCommentService.removeById(id);
        return ResultUtils.success("删除成功",id);
    }

    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
    @ApiOperation(value = "根据id集合删除")
    @DeleteMapping(value = "/delete")
    public ResultUtils<List<String>> deleteByIds(@RequestBody List<String> ids) {
        periodStudentCommentService.removeByIds(ids);
        return ResultUtils.success("删除成功",ids);
    }


    @ApiOperation(value = "根据id查询评论和教师信息")
    @GetMapping(value = "/getById/{id}}")
    public ResultUtils<PeriodStudentCommentAndTeacherVO> getCommentAndTeacherById(@PathVariable String id) {
        return ResultUtils.success(periodStudentCommentService.getCommentAndTeacherById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/page")
    public ResultUtils<Page<PeriodStudentCommentAndTeacherVO>> getCommentAndTeacherByPage(@RequestBody PagePARAM pagePARAM) throws IllegalAccessException {
        Page<PeriodStudentCommentAndTeacherVO> page = new Page(pagePARAM.getPageNum(),pagePARAM.getPageSize());
        QueryWrapper<PeriodStudentCommentAndTeacherVO> qw =  super.getQueryWrapperByPageParam(pagePARAM);

        return ResultUtils.success(periodStudentCommentService.getCommentAndTeacherPage(page,qw));
    }

    @ApiOperation(value = "点赞(取消点赞)")
    @PostMapping(value = "/like")
    public ResultUtils<String> like(String commentId) {
        periodStudentCommentService.like(commentId);
        return ResultUtils.success();
    }

}
